package com.bnlr.lrparking.appui.carplace;

import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.utils.DistanceUtil;
import com.bnlr.lrparking.appui.login.VerificaActivity;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.like.LikeButton;
import com.like.OnLikeListener;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.CarPlaceData;
import com.bnlr.lrparking.model.CarPlaceDetailM;
import com.bnlr.lrparking.model.CarPlaceM;
import com.bnlr.lrparking.model.Location;
import com.bnlr.lrparking.widget.NativeDialog;
import com.bnlr.lrparking.constant.Params;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/6.
 * 地址信息  地图
 */

public class CarPlaceDetailActivity extends BaseActivity {

    @BindView(R.id.carplace_mapView)
    MapView carplaceMapView;
    @BindView(R.id.iv_carplace_location)
    ImageView ivCarplaceLocation;
    Double lat, lon;
    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.iv_nav_back)
    ImageView ivNavBack;
    @BindView(R.id.iv_my_location)
    ImageView ivMyLocation;
    @BindView(R.id.zoomin)
    Button zoomin;
    @BindView(R.id.zoomout)
    Button zoomout;
    @BindView(R.id.tv_nav_right)
    TextView tvNavRight;
    @BindView(R.id.iv_nav_right)
    ImageView ivNavRight;
    @BindView(R.id.tv_carplace_name)
    TextView tvCarplaceName;
    @BindView(R.id.tv_mianfei)
    TextView tvMianfei;
    @BindView(R.id.tv_jiamengzhong)
    TextView tvJiamengzhong;
    @BindView(R.id.tv_car_juli)
    TextView tvCarJuli;
    @BindView(R.id.tv_car_kong)
    TextView tvCarKong;
    @BindView(R.id.tv_car_time)
    TextView tvCarTime;
//    @BindView(R.id.tv_car_time2)
//    TextView tvCarTime2;
    @BindView(R.id.tv_car_xiaohao)
    TextView tvCarXiaohao;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_collect)
    TextView tv_collect;
    @BindView(R.id.likeButton)
    LikeButton likeButton;
    @BindView(R.id.lay_carplace_info)
    LinearLayout layCarplaceInfo;
    private BaiduMap mBaiduMap;
    private GeoCoder mSearch;
    private LatLng mCenterLatLng;
    private LatLng mOldLatLng;
    CarPlaceData carplace;


    String address;
    View view;
    TextView marker_name;
    ImageView marker_img;
    BitmapDescriptor mCurrentMarker;
    private SensorManager mSensorManager;
    private Double lastX = 0.0;
    private MyLocationConfiguration.LocationMode mCurrentMode;
    private CarPlaceData intentParkSpace;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_carplace_detail);
        ButterKnife.bind(this);
        initView();
        carplace = (CarPlaceData) getIntent().getSerializableExtra("CarPlace");
        intentParkSpace = carplace;
        tvNavTitle.setText("地址");
        lat = Double.parseDouble(carplace.getLat());
        lon = Double.parseDouble(carplace.getLng());
        mCenterLatLng=new LatLng(lat,lon);
        getCarData();
        //initCarPlace();
        LatLng ll = new LatLng(lat, lon);
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
        mBaiduMap.animateMapStatus(u);
        getData();
        Listener();
    }

    private void initCarPlace() {
        tvCarplaceName.setText(carplace.getName());
        if ("0".equals(carplace.getIsjoin())) {
            tvMianfei.setVisibility(View.VISIBLE);
            tvJiamengzhong.setVisibility(View.GONE);
            tvCarKong.setVisibility(View.VISIBLE);
            tvCarKong.setText("开放时间：" + carplace.getOpentime());
            layCarplaceInfo.setVisibility(View.VISIBLE);
        } else {
            tvMianfei.setVisibility(View.GONE);
            tvCarKong.setVisibility(View.VISIBLE);
            tvCarKong.setText("开放时间：24小时开放");
            tvJiamengzhong.setVisibility(View.VISIBLE);
            layCarplaceInfo.setVisibility(View.GONE);
        }
        if(AppConfig.getInstance().getBoolean("isLogin",false)&&"1".equals(carplace.getIsFavorite())){
            likeButton.setLiked(true);
            tv_collect.setTextColor(getResources().getColor(R.color.main_color));
        }else{
            likeButton.setLiked(false);
            tv_collect.setTextColor(getResources().getColor(R.color.black_color));
        }
        tvNavTitle.setText(carplace.getName());
        tvCarJuli.setText("距离：" + carplace.getJuli());
        tvCarTime.setText("消耗：" + carplace.getSharevalue() + "共享值/小时");
        tvCarXiaohao.setText("空车位：" + carplace.getChewei());
        String phone=carplace.getPhone();
        if(TextUtils.isEmpty(phone)){
            phone="暂无";
        }
        tvPhone.setText("电话：" + phone);
        tvAddress.setText("地址：" + carplace.getAddress());
    }
    private void initCarPlaceDetail() {
        tvCarplaceName.setText(placeDetail.getName());
        if ("0".equals(placeDetail.getIsjoin())) {
            tvMianfei.setVisibility(View.VISIBLE);
            tvJiamengzhong.setVisibility(View.GONE);
            tvCarTime.setVisibility(View.VISIBLE);
            layCarplaceInfo.setVisibility(View.VISIBLE);
        } else {
            tvMianfei.setVisibility(View.GONE);
            tvCarTime.setVisibility(View.GONE);
            tvJiamengzhong.setVisibility(View.VISIBLE);
            layCarplaceInfo.setVisibility(View.GONE);
        }
     if(AppConfig.getInstance().getBoolean("isLogin",false)&&"1".equals(placeDetail.getIsFavorite())){
            likeButton.setLiked(true);
            tv_collect.setTextColor(getResources().getColor(R.color.main_color));
        }else{
            likeButton.setLiked(false);
            tv_collect.setTextColor(getResources().getColor(R.color.black_color));
        }
        tvNavTitle.setText(placeDetail.getName());
        tvCarJuli.setText("距离：" + placeDetail.getJuli());
        tvCarKong.setText("空车位：" + placeDetail.getChewei());
        tvCarTime.setText("开放时间：" + placeDetail.getOpentime());
//        tvCarTime2.setText("开放时间：" + placeDetail.getOpentime());
        tvCarXiaohao.setText("消耗：" + placeDetail.getSharevalue() + "共享值/小时");
        String phone=placeDetail.getPhone();
        if(TextUtils.isEmpty(phone)){
            phone="暂无";
        }
        tvPhone.setText("电话：" + phone);
        tvAddress.setText("地址：" + placeDetail.getAddress());
    }

    private void Listener() {
        likeButton.setOnLikeListener(new OnLikeListener() {
            @Override
            public void liked(LikeButton likeButton) {
                if (!AppConfig.getInstance().getBoolean("isLogin", false)) {
                   startActivity(VerificaActivity.class);
                }else{
                    SendCollect(true);
                    tv_collect.setTextColor(getResources().getColor(R.color.main_color));
                }
            }

            @Override
            public void unLiked(LikeButton likeButton) {
                if (!AppConfig.getInstance().getBoolean("isLogin", false)) {
                    startActivity(VerificaActivity.class);
                } else {
                    SendCollect(false);
                    tv_collect.setTextColor(getResources().getColor(R.color.black_color));
                }
            }
        });
        //调用BaiduMap对象的setOnMarkerDragListener方法设置marker的监听
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {

                BitmapDescriptor bitmap;
                for (int i = 0; i < carplaceList.size(); i++) {//carplaceList这个是数据
                    //判断经纬度
                    if (marker.getPosition().latitude == Double.valueOf(carplaceList.get(i).getLat()) && marker.getPosition().longitude == Double.valueOf(carplaceList.get(i).getLng())) {
                        carplace = carplaceList.get(i);
                        if (carplace.getId().equals(intentParkSpace.getId())) {

                            carplace.setIsFavorite(intentParkSpace.getIsFavorite());

                        }
                        break;
                    }
                }

                initCarPlace();
                return false;
            }
        });
        mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
            /**
             * 手势操作地图，设置地图状态等操作导致地图状态开始改变。
             * 地图状态改变开始时的地图状态
             */
            @Override
            public void onMapStatusChangeStart(MapStatus status) {
                Log.i("onMapStatusChangeStart", "onMapStatusChangeStart");
                //tvNavTitle.setVisibility(View.INVISIBLE);
            }

            /**
             * 地图状态改变结束
             * @param status
             *            地图状态改变结束后的地图状态
             */
            @Override
            public void onMapStatusChangeFinish(MapStatus status) {
                Log.i("onMapStatusChangeFinish", "onMapStatusChangeFinish");
                mOldLatLng = status.target;
                /** 获取中心经纬度 */
                if (DistanceUtil.getDistance(mOldLatLng, mCenterLatLng) > 200) {
                    mCenterLatLng = status.target;
                    mSearch.reverseGeoCode(new ReverseGeoCodeOption().location(mCenterLatLng));
                }
            }

            /**
             * 地图状态变化中
             * @param status
             *            当前地图状态
             */
            @Override
            public void onMapStatusChange(MapStatus status) {
                Log.i("onMapStatusChange", "onMapStatusChange");
            }
        });
    }


    public void initView() {
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);//获取传感器管理服务
        mBaiduMap = carplaceMapView.getMap();
        //设置地图缩放级别，百度地图将地图的级别定义为（3~19）
        mBaiduMap.animateMapStatus(MapStatusUpdateFactory.zoomTo(18));
        //普通地图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        //隐藏百度Logo
        carplaceMapView.getChildAt(1).setVisibility(View.INVISIBLE);
        //地理编码功能：初始化模块，注册事件监听
        //展示如何进行地理编码搜索（用地址检索坐标）、反地理编码搜索（用坐标检索地址）
        mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING;
        mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(
                mCurrentMode, true, mCurrentMarker));
        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(new MyGetGeoCoderResultListener());
        // 隐藏百度的LOGO
        View child = carplaceMapView.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);
        }
        // 不显示地图上比例尺
        carplaceMapView.showScaleControl(false);
        carplaceMapView.showZoomControls(false);

    }

    List<CarPlaceData> carplaceList = new ArrayList<>();
    boolean isTure =false;

    @Override
    public void getData() {
        super.getData();
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "GetParkByLocation");
            mRequest.add("m", "200");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("lng", lon);
            mRequest.add("lat", lat);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            // 添加到请求队列+
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<CarPlaceM>(baseContext, true, CarPlaceM.class) {
                        @Override
                        public void doWork(CarPlaceM data, String code) {
                            if ("1".equals(code)) {
                                carplaceList.clear();
                                carplaceList.addAll(data.getData());
                                mBaiduMap.clear(); // 重置mBaiduMap
                                for (int i = 0; i < carplaceList.size(); i++) {
                                    CarPlaceData carplace = carplaceList.get(i);
                                    if ("0".equals(carplace.getIsjoin())) {
                                        if (Integer.valueOf(carplace.getChewei()) > 10) {
                                            view = LayoutInflater.from(CarPlaceDetailActivity.this).inflate(R.layout.layout_marker, null);
                                            marker_name = (TextView) view.findViewById(R.id.marker_name);
                                            marker_img = (ImageView) view.findViewById(R.id.marker_img);
                                            marker_name.setText(carplace.getChewei());
                                            marker_name.setTextColor(getResources().getColor(R.color.main_color));
                                            marker_img.setImageResource(R.mipmap.home_cooper1_nor);
                                            mBaiduMap.addOverlay(new MarkerOptions().position(new LatLng(Double.valueOf(carplace.getLat()), Double.valueOf(carplace.getLng())))
                                                    .icon(BitmapDescriptorFactory.fromBitmap(getViewBitmap(view))));
                                        } else {
                                            view = LayoutInflater.from(CarPlaceDetailActivity.this).inflate(R.layout.layout_marker, null);
                                            marker_name = (TextView) view.findViewById(R.id.marker_name);
                                            marker_img = (ImageView) view.findViewById(R.id.marker_img);
                                            marker_name.setText(carplace.getChewei());
                                            marker_img.setImageResource(R.mipmap.home_cooper2_nor);
                                            marker_name.setTextColor(getResources().getColor(R.color.red_color));
                                            mBaiduMap.addOverlay(new MarkerOptions().position(new LatLng(Double.valueOf(carplace.getLat()), Double.valueOf(carplace.getLng())))
                                                    .icon(BitmapDescriptorFactory.fromBitmap(getViewBitmap(view))));
                                        }
                                    } else if ("1".equals(carplace.getIsjoin())) {
                                        view = LayoutInflater.from(CarPlaceDetailActivity.this).inflate(R.layout.layout_marker, null);

                                        marker_name = (TextView) view.findViewById(R.id.marker_name);
                                        marker_img = (ImageView) view.findViewById(R.id.marker_img);
                                        marker_img.setImageResource(R.mipmap.home_noncooper_nor);
                                        mBaiduMap.addOverlay(new MarkerOptions().position(new LatLng(Double.valueOf(carplace.getLat()), Double.valueOf(carplace.getLng())))
                                                .icon(BitmapDescriptorFactory.fromBitmap(getViewBitmap(view))));
                                    }
                                }
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (!TextUtils.equals("1", code)) {
                                    carplaceList.clear();
                                    mBaiduMap.clear();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }

    }
    private void SendCollect(final boolean isCollect) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "SetFavorite");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid",""));
            mRequest.add("parkId", carplace.getId());
            // 添加到请求队列
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<JSONObject>(baseContext, false, null) {
                        @Override
                        public void doWork(JSONObject data, String code) {

                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    if (isCollect) {
                                        if (carplace.getId().equals(intentParkSpace.getId())) {
                                            intentParkSpace.setIsFavorite("1");
                                        }
                                        carplace.setIsFavorite("1");
                                        tv_collect.setTextColor(getResources().getColor(R.color.main_color));
                                    } else {
                                        if (carplace.getId().equals(intentParkSpace.getId())) {
                                            intentParkSpace.setIsFavorite("0");
                                        }
                                        carplace.setIsFavorite("0");
                                        tv_collect.setTextColor(getResources().getColor(R.color.black_color));
                                    }
                                } else {
                                    CommonUtil.showToask(baseContext, obj.getString("msg"));
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {

        }
    }
    float zoomLevel;

    @Override
    public void doClick(View v) {
        super.doClick(v);
        switch (v.getId()) {
            case R.id.iv_my_location:
                LatLng ll = new LatLng(Params.CurrentLat, Params.CurrentLon);
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); //定位点移至中心
                mBaiduMap.animateMapStatus(u);
                break;
            case R.id.zoomin:
                zoomLevel = mBaiduMap.getMapStatus().zoom;
                if (zoomLevel <= 18) {
                    mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomIn());
                    zoomout.setEnabled(true);
                } else {
                    CommonUtil.showToask(baseContext, "已经放至最大！");
                    zoomin.setEnabled(false);
                }
                break;
            case R.id.zoomout:
                zoomLevel = mBaiduMap.getMapStatus().zoom;
                if (zoomLevel > 4) {
                    mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomOut());
                    zoomin.setEnabled(true);
                } else {
                    zoomout.setEnabled(false);
                    CommonUtil.showToask(baseContext, "已经缩至最小！");
                }
                break;
            case R.id.lay_daohang:
                /**
                 * 先判断是不是登陆，检测地图工具
                 */
                if (AppConfig.getInstance().getBoolean("isLogin", false)) {
                    Location loc_now = new Location(Params.CurrentLat, Params.CurrentLon, "s");
                    Location loc_end = new Location(Double.valueOf(carplace.getLat()), Double.valueOf(carplace.getLng()), "e");
                    NativeDialog msgDialog = new NativeDialog(CarPlaceDetailActivity.this, loc_now, loc_end);
                    msgDialog.show();
                } else {
                   startActivity(VerificaActivity.class);
                }
                break;
            case R.id.lay_callphone:
                if (!AppConfig.getInstance().getBoolean("isLogin", false)) {
                    startActivity(VerificaActivity.class);
                } else {
                    if(TextUtils.isEmpty(carplace.getPhone())){
                        CommonUtil.showToask(baseContext,"暂无电话，无法拨打");
                        break;
                    }
                    final NormalDialog dialog = new NormalDialog(CarPlaceDetailActivity.this);
                    dialog.title("提示")
                            .titleTextColor(getResources().getColor(R.color.black_color))
                            .titleTextSize(15)
                            .content("拨打电话" + carplace.getPhone() + "？")
                            .contentTextSize(15)
                            .contentTextColor(getResources().getColor(R.color.gray_color))
                            .btnText("取消", "确定")
                            .btnNum(2)
                            .style(NormalDialog.STYLE_TWO)
                            .btnTextColor(getResources().getColor(R.color.gray_color), getResources().getColor(R.color.colorAccent))
                            .btnTextSize(15, 15)
                            .widthScale(0.8f)//
                            .showAnim(new BounceTopEnter())//
                            .dismissAnim(new SlideBottomExit())//
                            .cornerRadius(7)
                            .show();
                    dialog.setCancelable(true);
                    dialog.setOnBtnClickL(
                            new OnBtnClickL() {
                                @Override
                                public void onBtnClick() {
                                    dialog.dismiss();
                                }
                            },
                            new OnBtnClickL() {
                                @Override
                                public void onBtnClick() {
                                    dialog.dismiss();
                                    Intent intent = new Intent();
                                    intent.setAction(Intent.ACTION_CALL);
                                    intent.setData(Uri.parse("tel:" + carplace.getPhone()));
                                    startActivity(intent);

                                }
                            });
                }
                break;
                default:
                    break;
        }
    }
    private void getPersonInfo() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "GetUserInfoById");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid",""));
            // 添加到请求队列
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<JSONObject>(baseContext, false, null) {
                        @Override
                        public void doWork(JSONObject data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {
                                    //{"msgcode":"1","msg":"成功","data":{"id":9,"userName":"15838315025"
                                    // ,"nickName":"","realName":"","password":"95AC5EF9FAD59065","salt":"628686",
                                    // "photo":"","status":0,"sharevalue":0,"sharevalue_u":0,"parktime":"0h"}}
                                    AppConfig.getInstance().putString("userName",obj.getJSONObject("data").getString("userName"));//用户名
                                    AppConfig.getInstance().putString("nickName",obj.getJSONObject("data").getString("nickName"));//昵称
                                    AppConfig.getInstance().putString("realName",obj.getJSONObject("data").getString("realName"));//真实姓名
                                    AppConfig.getInstance().putString("password",obj.getJSONObject("data").getString("password"));//密码
                                    AppConfig.getInstance().putString("salt",obj.getJSONObject("data").getString("salt"));//加密字符串
                                    AppConfig.getInstance().putString("photo",obj.getJSONObject("data").getString("photo"));//头像
                                    AppConfig.getInstance().putString("status",obj.getJSONObject("data").getString("status"));//用户状态 0 正常 1停用
                                    AppConfig.getInstance().putString("sharevalue",obj.getJSONObject("data").getString("sharevalue"));//共享值
                                    AppConfig.getInstance().putString("sharevalue_u",obj.getJSONObject("data").getString("sharevalue_u"));//已消耗共享值
                                    AppConfig.getInstance().putString("parktime",obj.getJSONObject("data").getString("parktime"));//已停车时间
                                    getCarData();
                                } else {
                                    CommonUtil.showToask(baseContext, obj.getString("msg"));
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {

        }
    }
    CarPlaceDetailM.DataBean placeDetail;
    private void getCarData() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "GetParkingById");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid",AppConfig.getInstance().getString("uid",""));
            mRequest.add("id", carplace.getId());
            mRequest.add("lng", lon);
            mRequest.add("lat", lat);
            // 添加到请求队列
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<CarPlaceDetailM>(baseContext, true, CarPlaceDetailM.class) {
                        @Override
                        public void doWork(CarPlaceDetailM data, String code) {
                            if("1".equals(code)) {
                                CarPlaceDetailM.DataBean db = data.getData();
                                carplace.setId(db.getId());
                                carplace.setAddress(db.getAddress());
                                carplace.setChewei(db.getChewei());
                                carplace.setIsFavorite(db.getIsFavorite());
                                carplace.setIsjoin(db.getIsjoin());
                                carplace.setJuli(db.getJuli());
                                carplace.setLat(db.getLat());
                                carplace.setLng(db.getLng());
                                carplace.setMaxsellingprice(db.getMaxsellingprice());
                                carplace.setMinsellingprice(db.getMinsellingprice());
                                carplace.setName(db.getName());
                                carplace.setOpentime(db.getOpentime());
                                carplace.setPhone(db.getPhone());
                                carplace.setSellingcount(db.getSellingcount());
                                carplace.setSharevalue(db.getSharevalue());

                                initCarPlace();
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if ("1".equals(code)) {

                                } else {
                                    CommonUtil.showToask(baseContext, obj.getString("msg"));
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {

        }
    }

    private Bitmap getViewBitmap(View addViewContent) {
        addViewContent.setDrawingCacheEnabled(true);
        addViewContent.measure(
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        addViewContent.layout(0, 0,
                addViewContent.getMeasuredWidth(),
                addViewContent.getMeasuredHeight());
        addViewContent.buildDrawingCache();

        Bitmap cacheBitmap = addViewContent.getDrawingCache();
        Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);

        return bitmap;
    }

    /**
     * 地理编码监听器
     */
    private class MyGetGeoCoderResultListener implements OnGetGeoCoderResultListener {
        @Override
        public void onGetGeoCodeResult(GeoCodeResult result) {  //地址转坐标
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                CommonUtil.showToask(CarPlaceDetailActivity.this, "抱歉，未能找到结果");
                return;
            }
        }

        @Override
        public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) { //反Geo搜索，坐标转地址
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                CommonUtil.showToask(CarPlaceDetailActivity.this, "抱歉，未能找到结果");
                return;
            }
            address = result.getAddress();
            lat= result.getLocation().latitude;
            lon = result.getLocation().longitude;
            getData();
            //tvNavTitle.setVisibility(View.VISIBLE);
//            tvNavTitle.setText(address);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        carplaceMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        carplaceMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        carplaceMapView.onDestroy();
        carplaceMapView = null;
        mSearch.destroy();
    }

}
